#!/usr/bin/env python
# -*- coding: utf-8 -*- #   
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
from google.appengine.api import users
import Cookie,urllib
from django.utils import simplejson
from google.appengine.api import urlfetch

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp.util import run_wsgi_app
import time
from google.appengine.ext import db
from google.appengine.api import mail

import SiteType
class ShowLog(webapp.RequestHandler):
	def get(self):
		user=SiteType.Header(self,u"Logs")
		self.response.out.write('<div align="center">')
		if user:
			if user.email()==SiteType.superadminname:
				logs=db.GqlQuery("SELECT * FROM Log ORDER BY date DESC LIMIT 100")
				for log in logs:
					if log.user:
						self.response.out.write(u"%s"%log.user.email())
					else:
						self.response.out.write(u"%s"%log.ip)
					self.response.out.write(u" came at %s<br>"%log.date)
			else:
				self.response.out.write(u"对不起，你无权查看此超级管理员页面")
		else:
			self.response.out.write(u"对不起，你无权查看此超级管理员页面")
		SiteType.Ender(self)
class superadmin(webapp.RequestHandler):
	def get(self):
		user=SiteType.Header(self,u"超级管理员界面")
		self.response.out.write('<div align="center">')
		if user:
			if user.email()==SiteType.superadminname:
				members=db.GqlQuery("SELECT * FROM Member")
				for member in members:
					self.response.out.write(u"%s&nbsp;&nbsp;&nbsp;&nbsp;"%member.name_chs+u"%s"%member.username.email())
					if member.isadmin:
						self.response.out.write(u"""<a href="/superadmin/del?user=%s">撤销此管理员</a><br>"""%member.key())
					else:
						self.response.out.write(u"""<a href="/superadmin/add?user=%s">增添此管理员</a><br>"""%member.key())
			else:
				self.response.out.write(u"对不起，你无权查看此超级管理员页面")
		else:
			self.response.out.write(u"对不起，你无权查看此超级管理员页面")
		SiteType.Ender(self)
		
class AddAdmin(webapp.RequestHandler):
	def get(self):
		url=self.request.uri
		thekey=SiteType.getkey(url,'?user=')
		member=db.get(thekey)
		user=users.get_current_user()
		if user:
			if user.email()==SiteType.superadminname:
				member.isadmin=True
				member.put()
				mail.send_mail(sender=SiteType.superadminname,
              to=member.username.email(),
              subject=u"您被增添为管理员",
              body=u"""
%s:

您好，您被增添为本站的管理员。您可以以后登录网站时点击 管理员页面 进行管理员操作

http://xgdfzor.appspot.com

祝好
本站
"""%member.name_chs)
		self.redirect(self.request.headers['referer'])
class DelAdmin(webapp.RequestHandler):
	def get(self):
		url=self.request.uri
		thekey=SiteType.getkey(url,'?user=')
		mem=db.get(thekey)	
		user=users.get_current_user()
		if user:
			if user.email()==SiteType.superadminname:
				mem.isadmin=False
				mem.put()
				mail.send_mail(sender=SiteType.superadminname,
              to=mem.username.email(),
              subject=u"您被取消管理员资格",
              body=u"""
%s:

您好，您被取消本站的管理员资格。

http://xgdfzor.appspot.com

祝好
本站
"""%mem.name_chs)
		self.redirect(self.request.headers['referer'])

def main():
  application = webapp.WSGIApplication([('/superadmin', superadmin),('/superadmin/add.*',AddAdmin),('/superadmin/del.*',DelAdmin),('/log',ShowLog)],
                                       debug=True)
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()
